﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TvViewer.MpTvService;

namespace TvViewer
{
  public partial class RecordingManualInputBox : Form
  {
    #region private properties
    ServiceInterface m_server;
    private String m_recordingName;
    private int m_startEarlier;
    private int m_endLater;
    WebChannel[] m_channels;
    WebChannelGroup[] m_groups;
    private WebChannel m_selectedChannel;
    private DateTime m_beginTime;
    private DateTime m_endTime;
    #endregion

    public DateTime EndTime
    {
      get { return m_endTime; }
      set { m_endTime = value; }
    }

    public DateTime BeginTime
    {
      get { return m_beginTime; }
      set { m_beginTime = value; }
    }

    public WebChannel SelectedChannel
    {
      get { return m_selectedChannel; }
      set { m_selectedChannel = value; }
    }

    public int StartEarlier
    {
      get { return m_startEarlier; }
      set { m_startEarlier = value; }
    }

    public int EndLater
    {
      get { return m_endLater; }
      set { m_endLater = value; }
    }

    public String RecordingName
    {
      get { return m_recordingName; }
      set { m_recordingName = value; }
    }

    public RecordingManualInputBox(ServiceInterface _server)
    {
      InitializeComponent();
      m_server = _server;
      m_groups = m_server.GetTvChannelGroups();

      cbGroups.Items.Clear();
      if (m_groups != null)
      {
        for (int i = 0; i < m_groups.Length; i++)// (WebChannelGroup c in groups)
        {
          cbGroups.Items.Add(m_groups[i].name);
        }
      }
    }

    private void cmdOk_Click(object sender, EventArgs e)
    {
      try
      {
        int beginHour = txtBeginHour.Text.Equals("") ? 0 : Int32.Parse(txtBeginHour.Text);
        int beginMinute = txtBeginMinutes.Text.Equals("") ? 0 : Int32.Parse(txtBeginMinutes.Text);

        int endHour = txtEndHour.Text.Equals("") ? 0 : Int32.Parse(txtEndHour.Text);
        int endMinute = txtEndMinutes.Text.Equals("") ? 0 : Int32.Parse(txtEndMinutes.Text);

        m_beginTime = new DateTime(dateTimePickerBegin.Value.Year, dateTimePickerBegin.Value.Month,
                                   dateTimePickerBegin.Value.Day, beginHour, beginMinute, 0);

        m_endTime = new DateTime(dateTimePickerEnd.Value.Year, dateTimePickerEnd.Value.Month,
                                 dateTimePickerEnd.Value.Day, endHour, endMinute, 0);

        m_selectedChannel = m_channels[cbChannels.SelectedIndex];

        m_recordingName = txtRecordingName.Text;
        this.DialogResult = DialogResult.OK;
      }
      catch (Exception ex)
      {
        MessageBox.Show("Please enter valid values");
      }


    }

    private void cmdCancel_Click(object sender, EventArgs e)
    {
      this.DialogResult = DialogResult.OK;
    }

    private void cgGroups_SelectedIndexChanged(object sender, EventArgs e)
    {
      FillChannelsDropDownList();
    }

    private void FillChannelsDropDownList()
    {
      int index = cbGroups.SelectedIndex;

      WebChannelGroup selectedChannel = m_groups[index];

      m_channels = m_server.GetChannelsInTvGroup(selectedChannel.idGroup);

      cbChannels.Items.Clear();
      for (int i = 0; i < m_channels.Length; i++)
      {
        String name = m_channels[i].displayName;
        cbChannels.Items.Add(name);
      }
    }
  }
}
